using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using IGlobalist.Cms.Model;


namespace IGlobalist.Cms.Bll
{
    public class CategoryController : BaseController
    {
        public static IContentCategory ConvertCmsObject2Category(ICmsObject obj)
        {
            IContentCategory category = new Category();
            category.FromCmsObject(obj);
            
            return category;
        }

        private static IList<IContentCategory> ConvertListObject2ListCategories(IList<ICmsObject> list)
        {
            IList<IContentCategory> categories = new List<IContentCategory>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                categories.Add(ConvertCmsObject2Category(obj));
            }

            return categories;
        }

        public static ContentCategoryList ConvertListObject2ContentCategoryList(IList<ICmsObject> list)
        {
            
            ContentCategoryList categoryList = new ContentCategoryList();
            if (list != null)
            foreach (ICmsObject obj in list)            
            {
                if (obj.ClassTypeID == (short)CmsObjectType.ContentCategory)
                {
                    categoryList.Add(ConvertCmsObject2Category(obj));
                }
            }
             return categoryList;
        }

        public static IContentCategory GetCategory(long id)
        {
            return ConvertCmsObject2Category(_Ds.GetObject(id));
        }

        public static ContentCategoryList GetCategoryListLelateToObjectID(long objectID)
        {
            return ConvertListObject2ContentCategoryList(_Ds.GetObjectListRelateToObjectID(objectID, (short)CmsObjectType.ContentCategory));
        }

        public static IList<IContentCategory> GetCategoryList(long classID)
        {
            return ConvertListObject2ListCategories(_Ds.GetObjectListByClassID(classID));
        }

        public static ContentCategoryList GetCategoryRootList(bool isSubCategory)
        {
            IList<ICmsClass> list=ClassController.GetClassListByClassTypeID((short)CmsObjectType.ContentCategory);
            ContentCategoryList categories = new ContentCategoryList();
            
            IList<ICmsObject> listObject = _Ds.GetObjectListByClassID(list[0].ID);
            foreach (ICmsObject obj in listObject)
            {
                if (obj.ParentID == 0 )
                {
                    IContentCategory category = ConvertCmsObject2Category(obj);
                    if (isSubCategory)
                    {
                        category.CategoryList = GetCategoryListLelateToObjectID(obj.ID);
                    }
                    categories.Add(category);             
                }
            }

            return categories;
        }
        /// <summary>
        /// Get Category for bind 
        /// </summary>
        /// <returns></returns>
        public static IContentCategory GetCategoryTree()
        {
            IContentCategory category = new Category();
            ICmsObject objRoot = _Ds.GetObjectListTree((short)CmsObjectType.ContentCategory, false);
            category = ConvertCmsObject2Category(objRoot);
            PopulateList(objRoot, category);

            return category;
        }

        public static IContentCategory GetCategoryTreeByClassID(long classID)
        {
            IContentCategory category = new Category();
            ICmsObject objRoot = _Ds.GetObjectListTree((short)CmsObjectType.ContentCategory, false);
            category = ConvertCmsObject2Category(objRoot);
            PopulateListByClassID(objRoot, category,classID);
            return category;
        }

        public static IContentCategory GetCategoryTreeByUserID(long classID,long userID)
        {
            IContentCategory category = new Category();
            ICmsObject objRoot = _Ds.GetObjectListTree((short)CmsObjectType.ContentCategory, false);
            category = ConvertCmsObject2Category(objRoot);
            PopulateListByClassIDAndUserID(objRoot, category, classID,userID);
            return category;
        }

        private static void PopulateListByClassIDAndUserID(ICmsObject objRoot, IContentCategory category, long classID,long userID)
        {
            category.CategoryList = new ContentCategoryList();
            foreach (ICmsObject obj in objRoot.SubObjectList)
            {
                if ((obj.ClassTypeID == (short)CmsObjectType.ContentCategory) && (obj.ClassID == classID) && (obj.Author==userID))
                {
                    category.CategoryList.Add(ConvertCmsObject2Category(obj));
                    PopulateListByClassID(obj, category.CategoryList[category.CategoryList.Count - 1], classID);
                }

            }
        }

        private static void PopulateListByClassID(ICmsObject objRoot, IContentCategory category,long classID)
        {
            category.CategoryList = new ContentCategoryList();
            foreach (ICmsObject obj in objRoot.SubObjectList)
            {
                if ((obj.ClassTypeID == (short)CmsObjectType.ContentCategory) && (obj.ClassID==classID))
                {
                    category.CategoryList.Add(ConvertCmsObject2Category(obj));
                    PopulateListByClassID(obj, category.CategoryList[category.CategoryList.Count - 1], classID);
                }

            }
        }

        private static void PopulateList(ICmsObject objRoot, IContentCategory category)
        {
            category.CategoryList = new ContentCategoryList();
            foreach (ICmsObject obj in objRoot.SubObjectList)
            {
                if (obj.ClassTypeID == (short)CmsObjectType.ContentCategory)
                {
                    category.CategoryList.Add(ConvertCmsObject2Category(obj));
                    PopulateList(obj, category.CategoryList[category.CategoryList.Count - 1]);
                }

            }
        }

        public static IContentCategory GetCategoryNode(long id)
        {
            IContentCategory category = GetCategoryTree();
            IContentCategory catReturn = new Category();
            catReturn.ID = id;

            PopulateNode(category, ref catReturn);

            return catReturn;
        }

        private static void PopulateNode(IContentCategory objRoot, ref IContentCategory catReturn)
        {            
            foreach (IContentCategory obj in objRoot.CategoryList)
            {
                if (obj.ID == catReturn.ID)
                    catReturn=obj;
                PopulateNode(obj, ref catReturn);                                
            }


           
        }

        private static void PopulateBind(IContentCategory category, IList<IContentCategory> list, string prefix, long categoryID)
        {
            bool add = true;
            if (category.CategoryList != null)
            {
                prefix += "----";
                foreach (IContentCategory cat in category.CategoryList)
                {
                    //edit because page==forum
                    for (int i = 0; i <cat.ExtPropertyList.Count; i++)
                    {
                        if ((Convert.ToString(cat.ExtPropertyList[i].Name) == "TypeClass") && (Convert.ToString(cat.ExtPropertyList[i].Value) == "Forum"))
                        {
                            add = false;
                        }
                    }
                    if (add = true)
                    {
                        if (cat.ID != categoryID)
                        {
                            cat.Name = prefix + cat.Name;
                            list.Add(cat);
                            PopulateBind(cat, list, prefix, categoryID);
                        }
                    }
                    else
                        add = true;
                }                
            }
        }

        public static IList<IContentCategory> GetCategoryTreeForBind(long currentCategoryID, long categoryID)
        {
            IList<IContentCategory> list = new List<IContentCategory>();
            IContentCategory root = CategoryController.GetCategoryTree();
            list.Add(root);
            
            PopulateBind(root, list,"",categoryID);
            
            return list;
        }      

        public static IList<IContentCategory> GetCategoryListForBindTree(long classID, long currentID)
        {
            IList<IContentCategory> list = null;// ConvertListObject2ListCategories(_Ds.GetObjectListTree(classID, currentID));

            int n = 0;
            while (n < list.Count)
            {
                if (list[n].Name.IndexOf("---") != -1)
                    list.RemoveAt(n);
                else
                    n++;
            }
            return list;
        }

        public static long InsertCategory(IContentCategory category)
        {
            long id= _Ds.InsertObject((ICmsObject)category);
            
            return id;
        }

        public static void UpdateCategory(IContentCategory category)
        {            
            _Ds.UpdateObject((ICmsObject)category);

            
        }

        public static void DeleteCategory(long id)
        {
            _Ds.DeleteObject(id);

        }

    }
}
